package com.zhonghan.recharge.service.impl;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.enums.ResultStatus;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.OilCustomerOrderDao;
import com.zhonghan.recharge.dao.OilCustomerReleaseDao;
import com.zhonghan.recharge.pojo.OilCustomerOrder;
import com.zhonghan.recharge.pojo.OilCustomerRelease;
import com.zhonghan.recharge.service.OilCustomerReleaseService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OilCustomerReleaseServiceImpl implements OilCustomerReleaseService{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
    @Autowired
    private OilCustomerReleaseDao oilCustomerReleaseDao;
    @Autowired
    private OilCustomerOrderDao oilCustomerOrderDao;
    
    @Override
    public long getOilCustomerReleaseRowCount(){
        return oilCustomerReleaseDao.getOilCustomerReleaseRowCount();
    }
    @Override
    public List<OilCustomerRelease> selectOilCustomerRelease(){
        return oilCustomerReleaseDao.selectOilCustomerRelease();
    }
    @Override
    public List<OilCustomerRelease> selectOilCustomerReleaseByObj(OilCustomerRelease obj){
        return oilCustomerReleaseDao.selectOilCustomerReleaseByObj(obj);
    }
    @Override
    public OilCustomerRelease selectOilCustomerReleaseById(Integer id){
        return oilCustomerReleaseDao.selectOilCustomerReleaseById(id);
    }
    @Override
    public int insertOilCustomerRelease(OilCustomerRelease value){
        return oilCustomerReleaseDao.insertOilCustomerRelease(value);
    }
    @Override
    public int insertNonEmptyOilCustomerRelease(OilCustomerRelease value){
        return oilCustomerReleaseDao.insertNonEmptyOilCustomerRelease(value);
    }
    @Override
    public int insertOilCustomerReleaseByBatch(List<OilCustomerRelease> value){
        return oilCustomerReleaseDao.insertOilCustomerReleaseByBatch(value);
    }
    @Override
    public int deleteOilCustomerReleaseById(Integer id){
        return oilCustomerReleaseDao.deleteOilCustomerReleaseById(id);
    }
    @Override
    public int updateOilCustomerReleaseById(OilCustomerRelease enti){
        return oilCustomerReleaseDao.updateOilCustomerReleaseById(enti);
    }
    @Override
    public int updateNonEmptyOilCustomerReleaseById(OilCustomerRelease enti){
        return oilCustomerReleaseDao.updateNonEmptyOilCustomerReleaseById(enti);
    }

    public OilCustomerReleaseDao getOilCustomerReleaseDao() {
        return this.oilCustomerReleaseDao;
    }

    public void setOilCustomerReleaseDao(OilCustomerReleaseDao oilCustomerReleaseDao) {
        this.oilCustomerReleaseDao = oilCustomerReleaseDao;
    }
    
	
	/**
	 * 查询客户油品分期记录
	 * id: 客户充油订单id
	 */
	@Override
	public Result selectOilCustomerReleaseByCondition(Integer id, Integer pageNum, Integer pageSize) {
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? 10: pageSize;
		
		OilCustomerRelease oilCustomerRelease = new OilCustomerRelease();
		oilCustomerRelease.setCustomerOrderId(id);
		oilCustomerRelease.setOrderBy("create_time");
		oilCustomerRelease.setSortBy("desc");
		
		Result result = new Result();
		try {
			
			PageHelper.startPage(pageNum, pageSize);
			List<OilCustomerRelease> list = oilCustomerReleaseDao.selectOilCustomerReleaseByCondition(oilCustomerRelease);
			PageInfo<OilCustomerRelease> pageInfo = new PageInfo<>(list);
			result.setCode(ResultStatus.SUCCESS.getCode());
			result.setData(pageInfo);
			
		} catch (Exception e) {
			result.setCode(ResultStatus.BUSYNESS.getCode());
			result.setMsg(ResultStatus.BUSYNESS.getDescription());
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * 
	 * @param id				客户订单编号
	 * @param cardps0			1000面额充值卡开始号段
	 * @param cardpe0			1000面额充值卡结束号段
	 * @param cardps1			500面额充值卡开始号段
	 * @param cardpe1			500面额充值卡结束号段
	 * @param cardps2			100面额充值卡开始号段
	 * @param cardpe2			100面额充值卡结束号段
	 * @param releaseTime		释放时间
	 * @param releaseAmount		释放金额
	 * @return
	 * @throws Exception 
	 */
	@Override
	public Result addOilCustomerRelease(Integer id,String cardPart1000, String cardPart500, String cardPart100, String releaseTime,Double releaseAmount) throws Exception {
		Result result = new Result();
		
		OilCustomerOrder oilCustomerOrder = oilCustomerOrderDao.selectOilCustomerOrderById(id);
		if((oilCustomerOrder.getInputOilAmount()+oilCustomerOrder.getDonationAmount())/oilCustomerOrder.getBackByMonth() < releaseAmount){
			result.setFailure("-1", "释放的油品金额不能大于每月平均释放金额");
			return result;
		}
		
		OilCustomerRelease value = new OilCustomerRelease();
		value.setCardPart100(cardPart100);
		value.setCardPart500(cardPart500);
		value.setCardPart1000(cardPart1000);
		value.setCustomerOrderId(id);;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		value.setReleaseTime(sdf.parse(releaseTime));
		value.setReleaseAmount(releaseAmount);
		value.setCreateTime(new Date());
		
		try {
			int code = oilCustomerReleaseDao.insertNonEmptyOilCustomerRelease(value );
			if(code>0){
				result.setSuccess("1", null,"添加分期成功");
			}
		} catch (Exception e) {
			result.setFailure(ResultStatus.BUSYNESS.getCode(),ResultStatus.BUSYNESS.getDescription());
			e.printStackTrace();
		}
		
		return result;
	}
}